//
//  TwoWayMiniSlider.m
//  SKPAD
//
//  Created by admin@amonline.com.cn on 15/6/18.
//  Copyright (c) 2015年 amo. All rights reserved.
//

#import "TwoWayMiniSlider.h"

@interface TwoWayMiniSlider ()
{
    float _touchOffset;
    BOOL _haveAddedSubviews;
}

@property (retain, nonatomic) UIImageView* handle;
@property (retain, nonatomic) UIImageView* track;
@property (retain, nonatomic) UIImageView* trackBackground;

@end

@implementation TwoWayMiniSlider

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        [self configureView];
        
    }
    
    return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if(self)
    {
        [self configureView];
    }
    
    return self;
}
- (void) configureView
{
    //Setup the default values
    _minimumValue = 0.0;
    _maximumValue = 100.0;
    _midValue = 50.0;
    _continuous = YES;
    _handleValue = 70.0;
}

//初始化背景图和状态图
- (UIImage *)trackBackgroundImage
{
    if(_trackBackgroundImage==nil)
    {
        UIImage* image = [UIImage imageNamed:@"slider_mini_black_track.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 6.0, 0.0, 6.0)];
        _trackBackgroundImage = image;
    }
    
    return _trackBackgroundImage;
}

- (UIImage *)trackImage
{
    if(_trackImage==nil)
    {
        UIImage* image = [UIImage imageNamed:@"slider_mini_blue_track_center.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 2.0, 0.0, 2.0)];
        _trackImage = image;
    }
    return _trackImage;
}

- (UIImage *)handleImage
{
    if(_handleImage==nil)
    {
        UIImage* image = [UIImage imageNamed:@"slider_mini.png"];
        _handleImage = image;
    }
    return _handleImage;
}

-(float) valueForCenterX:(CGFloat) x
{
    float _padding = _handle.frame.size.width/2.0f;
    float value = _minimumValue + (x - _padding) / (self.frame.size.width - (_padding*2)) * (_maximumValue - _minimumValue);
    
    value = MAX(value, _minimumValue);
    value = MIN(value, _maximumValue);
    return value;
}

//获取背景槽的位置大小
-(CGRect) trackBackgroundRect
{
    CGRect trackBackgroundRect;
    
    trackBackgroundRect.size = CGSizeMake(_trackBackgroundImage.size.width, _trackBackgroundImage.size.height);
    
    if(_trackBackgroundImage.capInsets.top || _trackBackgroundImage.capInsets.bottom)
    {
        trackBackgroundRect.size.height=self.bounds.size.height;
    }
    
    if(_trackBackgroundImage.capInsets.left || _trackBackgroundImage.capInsets.right)
    {
        trackBackgroundRect.size.width=self.bounds.size.width;
    }
    
    trackBackgroundRect.origin = CGPointMake(0, (self.bounds.size.height/2.0f) - (trackBackgroundRect.size.height/2.0f));
    return trackBackgroundRect;
}

//返回中间到滑块间的计量条
- (CGRect)trackRect
{
    CGRect retValue;
    
    retValue.size = CGSizeMake(_trackImage.size.width, _trackImage.size.height);
    
    if(_trackImage.capInsets.top || _trackImage.capInsets.bottom)
    {
        retValue.size.height=self.bounds.size.height;
    }
    if(_handleValue < _midValue)
    {
        float xValue = ((self.bounds.size.width - _handle.frame.size.width / 2.0f) * (_handleValue - _minimumValue) / (_maximumValue - _minimumValue))+(_handle.frame.size.width/2.0f);
        retValue.origin = CGPointMake(xValue - _handle.frame.size.width / 2, (self.bounds.size.height/2.0f) - (retValue.size.height/2.0f));
        retValue.size.width = _trackBackground.frame.size.width / 2.0f - xValue + _handle.frame.size.width / 2;
    }
    else if(_handleValue > _midValue)
    {
        float xValue = ((self.bounds.size.width - _handle.frame.size.width / 2.0f) * (_maximumValue - _handleValue) / (_maximumValue - _minimumValue))+(_handle.frame.size.width/2.0f);
        retValue.origin = CGPointMake(_trackBackground.frame.size.width / 2, (self.bounds.size.height/2.0f) - (retValue.size.height/2.0f));
        retValue.size.width = _trackBackground.frame.size.width / 2.0f - xValue + _handle.frame.size.width / 2;
    }
    else
    {
        retValue.origin = CGPointMake(_trackBackground.frame.size.width / 2 - _trackImage.size.width / 2, (self.bounds.size.height/2.0f) - (retValue.size.height/2.0f));
    }
    
    return retValue;
}
//拇指所在的位置
- (CGRect)thumbRectForValue:(float)value
{
    CGRect thumbRect;
    UIEdgeInsets insets = _handleImage.capInsets;
    
    thumbRect.size = CGSizeMake(_handleImage.size.width, _handleImage.size.height);
    
    if(insets.top || insets.bottom)
    {
        thumbRect.size.height = self.bounds.size.height;
    }
    
    float xValue = ((self.bounds.size.width-thumbRect.size.width)*((value - _minimumValue) / (_maximumValue - _minimumValue)));
    thumbRect.origin = CGPointMake(xValue, (self.bounds.size.height/2.0f) - (thumbRect.size.height/2.0f));
    return thumbRect;
    
}

//把UIImage等内容放到control上去
- (void) addSubviews
{
    //------------------------------
    // Track Brackground
    self.trackBackground = [[UIImageView alloc] initWithImage:self.trackBackgroundImage];
    self.trackBackground.frame = [self trackBackgroundRect];
    
    //------------------------------
    // Track
    self.track = [[UIImageView alloc] initWithImage:self.trackImage];
    self.track.frame = [self trackRect];
    
    self.handle = [[UIImageView alloc] initWithImage:self.handleImage];
    self.handle.frame = [self thumbRectForValue:_handleValue];
    
    [self addSubview:self.trackBackground];
    [self addSubview:self.track];
    [self addSubview:self.handle];
}

//[self setNeedsLayout];会调用该方法。重新绘制位置
-(void)layoutSubviews
{
    
    if(_haveAddedSubviews==NO)
    {
        _haveAddedSubviews=YES;
        [self addSubviews];
    }
    
    
    self.trackBackground.frame = [self trackBackgroundRect];
    self.track.frame = [self trackRect];
    self.handle.frame = [self thumbRectForValue:_handleValue];
}


#pragma mark -
#pragma mark - Touch handling
//给滑块+大一点范围
- (CGRect) touchRectForHandle:(UIImageView*) handleImageView
{
    float xPadding = 10;
    float yPadding = 10; //(self.bounds.size.height-touchRect.size.height)/2.0f
    
    CGRect touchRect = handleImageView.frame;
    touchRect.origin.x -= xPadding/2.0;
    touchRect.origin.y -= yPadding/2.0;
    touchRect.size.height += xPadding;
    touchRect.size.width += yPadding;
    return touchRect;
}

-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    if(CGRectContainsPoint([self touchRectForHandle:_handle], touchPoint))
    {
        _handle.highlighted = YES;
        _touchOffset = touchPoint.x - _handle.center.x;
    }
    return YES;
}

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    float newValue = [self valueForCenterX:(touchPoint.x - _touchOffset)];
    if(_handle.highlighted)
    {
        [self setHandleValue:newValue];
    }
    
    if(_continuous)
    {
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    [self setNeedsLayout];
    return YES;
}

- (void) setHandleValue:(float)handleValue
{
    float value = handleValue;
    _handleValue = value;
    [self setNeedsLayout];
}

-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    _handle.highlighted = NO;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

@end